/*
This file is part of [ahttpserver] project. 

Author: Artem Kustikov (kustikoff[at]tut.by)


This code is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this code.

Permission is granted to anyone to use this code for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:

1. The origin of this code must not be misrepresented; you must
not claim that you wrote the original code. If you use this
code in a product, an acknowledgment in the product documentation
would be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original code.

3. This notice may not be removed or altered from any source
distribution.
*/

#ifndef AHTTP_ASPNET_WRAPPERS_HPP
#define AHTTP_ASPNET_WRAPPERS_HPP




namespace AHttp
{
	using namespace System;
	using namespace System::IO;
	using namespace System::Threading;
	using namespace System::Web;
	using namespace System::Web::Hosting;
	using namespace System::Diagnostics;
	
	using namespace System::Runtime::Remoting::Lifetime;
	using namespace System::Security;
	using namespace System::Security::Permissions;
	using namespace System::Security::Policy;
	using namespace System::Reflection;


	public interface class AspNetServer
	{
	public:
		void ReloadHost (int number, ApplicationShutdownReason shutdownReasonreason);
	};

	public ref class AspNetHost : MarshalByRefObject
	{
	public:
		/**
			The timeout for the ASP.Net runtime after which it is automatically unloaded when idle
			to release resources. Note this can't be externally set because the lease is set 
			during object construction. All you can do is change this property value here statically
		*/
		static int DefaultIdleTimeoutMinutes = 30;
		
		static int DefaultSponsorshipTimeout = 5;


	public:
		
		AspNetHost() : 
			_server (nullptr),
			_directoryNumber (-1),
			_requestsCount (0),
			_appDomain (nullptr)
		{}
		
		
	
		virtual Object^ InitializeLifetimeService() override
		{
			return nullptr; 
			
			//ILease^ lease = (ILease^) MarshalByRefObject::InitializeLifetimeService();
			//
			//if (lease->CurrentState == LeaseState::Initial
			//	|| lease->CurrentState == LeaseState::Renewing)
			//{
			//	lease->InitialLeaseTime = TimeSpan::FromMinutes(AspNetHost::DefaultIdleTimeoutMinutes);
			//	lease->RenewOnCallTime  = TimeSpan::FromMinutes(AspNetHost::DefaultIdleTimeoutMinutes);
			//	lease->SponsorshipTimeout = TimeSpan::FromMinutes(AspNetHost::DefaultSponsorshipTimeout);
			//}

			//return lease;

		}

		void Init (String^ installPath, AspNetServer^ server, int directoryNumber);
	
		void Stop ();

		bool MapClientScriptFileRequest (String^ path, String^ mappedPath); 
		
		void ProcessRequest (void* context);
        
        String^ ProcessSimpleRequest(String^ url);

		AppDomain^ GetDomain()					{	return _appDomain = AppDomain::CurrentDomain;	}
		IApplicationHost^ GetApplicationHost()	{	return HostingEnvironment::ApplicationHost;		}

		void Ping();

		long GetRequestsCount()			{	return _requestsCount;		}
		int GetDirectoryNumber()		{	return _directoryNumber;	}
		
	public:
		DateTime^ StartTimeStamp;


	protected:

		void OnAppDomainUnload(Object^ unusedObject, EventArgs^ unusedEventArgs);
		
		AspNetServer^ _server;
		int _directoryNumber;

	protected:
		String^ _physicalClientScriptPath;
		String^ _clientScriptPathV10;
		String^ _clientScriptPathV11;
		
		EventHandler^ _onAppDomainUnload;
		long	_requestsCount;
		AppDomain^ _appDomain;
		
	};

	[Serializable()]
	public ref class StateCheck : IRegisteredObject

	{
	public:
		StateCheck()
		{}

		virtual void Stop(bool immediate)
		{
		}

	};
}


#endif